package com.qdu.bean;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class RentCarBean implements WritableComparable<RentCarBean> {

    private String timestamp;
    private int registered;
    private int casual;
    private int count;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM");

    //空参构造
    public RentCarBean() {
        this.timestamp = "";
        this.registered=0;
        this.count=0;
        this.casual=0;
    }

    //序列化
    @Override
    public void write(DataOutput dataOutput) throws IOException {

        dataOutput.writeInt(registered);
        dataOutput.writeInt(casual);
        dataOutput.writeInt(count);
    }

    //反序列化
    @Override
    public void readFields(DataInput dataInput) throws IOException {

        registered = dataInput.readInt();
        casual = dataInput.readInt();
        count = dataInput.readInt();
    }

    //重写toString()方法


    @Override
    public String toString() {
        return "RentCarBean{" +
                "registered=" + registered +
                ", casual=" + casual +
                ", count=" + count +
                '}';
    }

    //setter
    public void set1(int registered,int casual,int count){
        this.registered = registered;
        this.casual = casual;
        this.count = count;
    }

    public void set2(String timestamp) {
        this.timestamp = timestamp;
    }


    //getter
    public int getRegistered() {
        return registered;
    }

    public int getCasual() {
        return casual;
    }

    public int getCount() {
        return count;
    }

    @Override
    public int compareTo(RentCarBean o) {
        try {
            long thisValue = sdf.parse(this.timestamp).getTime();
            long thatValue = sdf.parse(o.timestamp).getTime();
            return (thisValue < thatValue ? -1 :(thisValue == thatValue ? 0 : 1));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -2;
    }
}
